/**
 * Copyright 2011 Pete Cornish.
 * 
 * This file is part of log4droid.
 * 
 * log4droid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * log4droid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with log4droid.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.log4droid;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import android.util.Log;

/**
 * @author pete
 *
 */
public class Configuration {
	
	private static final String LOGGER_PROPERTIES = "/log4droid.xml";
	private static final String PREF_LOGGER_PREFIX = "prefixFullClassName";
	private static final boolean DEFAULT_LOGGER_PREFIX = false;
	private static final String PREF_SHOW_PACKAGE = "showPackage";
	private static final boolean DEFAULT_SHOW_PACKAGE = false;
	
	private volatile static boolean prefixFullClassName;
	private volatile static boolean showPackage;
	private volatile static boolean initialised;
	
	static {
		//set defaults
		prefixFullClassName = DEFAULT_LOGGER_PREFIX;
		showPackage = DEFAULT_SHOW_PACKAGE;
	}
	
	/**
	 * Initialise the configuration once only.
	 */
	static void initialise() {
		if (initialised) {
			return;
		}
		initialised = true;
		
		InputStream propertyStream = null;
		try {
			final Properties properties = new Properties();
			propertyStream = Configuration.class.getResourceAsStream(LOGGER_PROPERTIES);
			
			if (null != propertyStream) {
                properties.loadFromXML(propertyStream);
    			
                // attempt to read all successfully before setting
    			final boolean configPrefix = Boolean.parseBoolean(properties.getProperty(PREF_LOGGER_PREFIX, String.valueOf(DEFAULT_LOGGER_PREFIX)));
    			final boolean configPackage = Boolean.parseBoolean(properties.getProperty(PREF_SHOW_PACKAGE, String.valueOf(DEFAULT_SHOW_PACKAGE)));
    			
                setPrefixFullClassName(configPrefix);
                setShowPackage(configPackage);
    			
			} else {
	            // couldn't load preferences from file
	            Log.v("log4droid", "Unable to load log configuration from file: '" 
	                    + LOGGER_PROPERTIES + "' - using defaults");
			}
			
		} catch (Exception e) {
			// error loading preferences from file
			Log.w("log4droid", "Error loading log configuration from file: '" 
					+ LOGGER_PROPERTIES + "' - using defaults. " + e);
			
		} finally {
		    if (null != propertyStream) {
		        try {
                    propertyStream.close();
                } catch (IOException e) {
                    // sink
                }
		    }
		}
	}

	/**
	 * @return the showPackage
	 */
	public static boolean isShowPackage() {
		return showPackage;
	}

	/**
	 * @param showPackage the showPackage to set
	 */
	public static void setShowPackage(boolean showPackage) {
		Configuration.showPackage = showPackage;
	}

	/**
	 * @return the prefixFullClassName
	 */
	public static boolean getPrefixFullClassName() {
		return prefixFullClassName;
	}

	/**
	 * @param prefixFullClassName the prefixFullClassName to set
	 */
	public static void setPrefixFullClassName(boolean prefixFullClassName) {
		Configuration.prefixFullClassName = prefixFullClassName;
	}

	/**
	 * @return the initialised
	 */
	public static boolean isInitialised() {
		return initialised;
	}

	/**
	 * @param initialised the initialised to set
	 */
	public static void setInitialised(boolean initialised) {
		Configuration.initialised = initialised;
	}
}
